package com.example.dinner;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.dinner.sqlite.DatabaseHelper;
import com.example.dinner.sqlite.SQLiteActivity;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {

    Context context;
    List<ListBeans.ListBean> list;
    PassPosition passPosition;
    ArrayList<String> team = new ArrayList<>();
    ArrayList<String> nameOne = new ArrayList<>();
    ArrayList<String> nameTwo = new ArrayList<>();

    SQLiteDatabase db;
    private FragmentList fragmentList;

    public MyAdapter(Context context, List<ListBeans.ListBean> list) {
        this.context = context;
        this.list = list;
        getGroupFromDB();
    }

    private void getGroupFromDB() {
        //依靠DatabaseHelper带全部参数的构造函数创建数据库
        DatabaseHelper dbHelper = new DatabaseHelper(context, "test_db", null, 1);
        db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query("user", new String[]{"name"}, null, null, null, null, null);
        //利用游标遍历所有数据对象
        team.clear();
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            team.add(name);
        }
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.ly_ll_item, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, final int position) {
        holder.tv_dinner.setText(list.get(position).getContent().trim());
        holder.tv_no.setText(list.get(position).getStaff());
        holder.tv_name.setText(list.get(position).getSign());

        if (team.contains(list.get(position).getSign())) {
            holder.tv_name.setTextColor(Color.RED);
        } else {
            holder.tv_name.setTextColor(Color.BLACK);
        }
        if (passPosition != null) {

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    passPosition.pass(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {

        TextView tv_dinner;
        TextView tv_name;
        TextView tv_no;


        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tv_dinner = itemView.findViewById(R.id.tv_dinner);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_no = itemView.findViewById(R.id.tv_no);
        }
    }

    interface PassPosition {
        void pass(int position);
    }

    public void setOnClick(PassPosition passPosition) {
        this.passPosition = passPosition;
    }

}
